package com.iot.controller;

import com.iot.core.LResult;
import com.iot.core.PageData;
import com.iot.pojo.MRef;
import com.iot.service.RefService;
import com.iot.util.Common;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/ref/")
public class RefController extends BaseController {

    @Autowired
    private RefService service;


    @ResponseBody
    @RequestMapping("initProvince")
    public String initProvince() {
        try {
            List<MRef> list = service.initProvince();
            return LResult.Success(list).toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }

    @ResponseBody
    @RequestMapping("initProvinceBySelfId")
    public String initProvinceBySelfId() {
        try {
            List<MRef> list = service.initProvince();
            return LResult.Success(list).toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }

    @ResponseBody
    @RequestMapping("initCityById")
    public String initCityById(int provinceId) {
        try {
            List<MRef> list = service.initCityById(provinceId);
            return LResult.Success(list).toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }

    @ResponseBody
    @RequestMapping("initCityBySelfId")
    public String initCityBySelfId(int provinceId, int cityId) {
        try {
            List<MRef> list = service.initCityBySelfId(provinceId, cityId);
            return LResult.Success(list).toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }

    @ResponseBody
    @RequestMapping("initAreaById")
    public String initAreaById(int cityId) {
        try {

            List<MRef> list = service.initAreaById(cityId);
            return LResult.Success(list).toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }


    @ResponseBody
    @RequestMapping("initRegionById")
    public String initRegionById(int areaId, int appId) {
        try {

            List<MRef> list = service.initRegionById(areaId, appId);
            return LResult.Success(list).toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }

    @ResponseBody
    @RequestMapping("initSiteById")
    public String initSiteById(int regionId, int appId) {
        try {

            List<MRef> list = service.initSiteById(regionId, appId);
            return LResult.Success(list).toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }

    @ResponseBody
    @RequestMapping("initLineById")
    public String initLineById(int siteId, int appId) {
        try {

            List<MRef> list = service.initLineById(siteId, appId);
            return LResult.Success(list).toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }

    @ResponseBody
    @RequestMapping("initDeviceStatus")
    public String initDeviceStatus() {
        try {

            List<MRef> list = service.initDeviceStatus();
            return LResult.Success(list).toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }

    @ResponseBody
    @RequestMapping("initDeviceType")
    public String initDeviceType() {
        try {

            List<MRef> list = service.initDeviceType();
            return LResult.Success(list).toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }
    @ResponseBody
    @RequestMapping("initSensor")
    public String initSensor(int typeId) {
        try {

            List<MRef> list = service.initSensor(typeId);
            return LResult.Success(list).toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }
    @ResponseBody
    @RequestMapping("initWarnType")
    public String initWarnType(int typeId) {
        try {

            List<MRef> list = service.initWarnType(typeId);
            return LResult.Success(list).toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }
    @ResponseBody
    @RequestMapping("initDevice")
    public String initDevice() {
        try {
            PageData map = getPageData();
            if (!map.containsKey("appId")) {
                map.put("appId", 0);
            }
            if (!map.containsKey("sname")) {
                map.put("sname", "");
            }
            if (!map.containsKey("sdate")) {
                map.put("sdate", "");
            }
            if (!map.containsKey("edate")) {
                map.put("edate", "");
            } else {
                String strSdate = map.get("edate").toString();
                String tempdate = Common.getShortDate(Common.getSomeDay(strSdate, 1));
                map.put("edate", tempdate);
            }
            if (!map.containsKey("typeId")) {
                map.put("typeId", 0);
            }
            if (!map.containsKey("provinceId")) {
                map.put("provinceId", 0);
            }
            if (!map.containsKey("cityId")) {
                map.put("cityId", 0);
            }
            if (!map.containsKey("areaId")) {
                map.put("areaId", 0);
            }
            if (!map.containsKey("regionId")) {
                map.put("regionId", 0);
            }
            if (!map.containsKey("siteId")) {
                map.put("siteId", 0);
            }
            if (!map.containsKey("lineId")) {
                map.put("lineId", 0);
            }
            List<MRef> list = service.initDevice(map);
            return LResult.Success(list).toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }

}
